package edu.must.uh.Generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JavaBeanFileUtils {
    /**
     * 根据字段信息生成java属性信息，如：var username-->private String username;相应的set和get方法源码
     * @param column    字段信息
     * @param convertor 类型转换器
     * @return  java属性的set/get方法
     */
    private static JavaFieldGetSet createFieldGetSetSRC(ColumnInfo column, TypeConvertor convertor){
        JavaFieldGetSet jfgs=new JavaFieldGetSet();
        //将字段转为java属性
        String javaFiledType= convertor.databaseType2JavaType(column.getDataType());
        String colunmName=StringUtils.underlineToSmallCamel(column.getName());
        //生成字段信息
        jfgs.setFieldInfo("\tprivate "+javaFiledType+" "+colunmName+";\n");
        //生成getter
        StringBuilder getSrc=new StringBuilder();
        getSrc.append("\tpublic "+javaFiledType+" get"+StringUtils.underlineToBigCamel(column.getName())+"(){\n");
        getSrc.append("\t\treturn "+colunmName+";\n");
        getSrc.append("\t}\n");
        jfgs.setGetInfo(getSrc.toString());
        //生成setter
        StringBuilder setSrc=new StringBuilder();
        setSrc.append("\tpublic void set"+StringUtils.underlineToBigCamel(column.getName())+"("+javaFiledType+" "+colunmName+"){\n");
        setSrc.append("\t\tthis."+colunmName+"="+colunmName+";\n");
        setSrc.append("\t}\n");
        jfgs.setSetInfo(setSrc.toString());
        return jfgs;
    }

    /**
     * 根据表信息生成java源码
     * @param tableInfo 表信息
     * @param convertor 数据类型转换器
     * @return  java类的源代码
     */
    private static String createJavaSrc(TableInfo tableInfo,TypeConvertor convertor){
        Map<String,ColumnInfo>columns=tableInfo.getColumns();
        List<JavaFieldGetSet> javaFields=new ArrayList<>();
        for (ColumnInfo columnInfo:columns.values()){
            JavaFieldGetSet javaFieldGetSet=createFieldGetSetSRC(columnInfo,convertor);
            javaFields.add(javaFieldGetSet);
        }
        StringBuilder src=new StringBuilder();
        //生成package语句
        src.append("package "+ DBHepler.getConf().getPoPackage()+".bean;\n");
        //生成import语句
        src.append("import java.sql.*;\n");
        src.append("import java.util.*;\n\n");
        //生成类声明语句
        src.append("public class "+StringUtils.underlineToBigCamel(tableInfo.getTname())+"{\n");
        //生成属性列表
        for (JavaFieldGetSet javaFieldGetSet:javaFields){
            src.append(javaFieldGetSet.getFieldInfo());
        }
        src.append("\n\n");
        //生成get方法列表
        for (JavaFieldGetSet javaFieldGetSet:javaFields){
            src.append(javaFieldGetSet.getGetInfo());
        }
        src.append("\n\n");
        //生成set方法列表
        for (JavaFieldGetSet javaFieldGetSet:javaFields){
            src.append(javaFieldGetSet.getSetInfo());
        }
        src.append("\n\n");
        //生成结束符
        src.append("}");
        return src.toString();
    }

    /**
     * 生成java文件
     * @param tableInfo 表信息
     * @param convertor 类型转换器
     */
    private static void createJavaPoFile(TableInfo tableInfo,TypeConvertor convertor){      
     
        String srcPath=DBHepler.getConf().getSrcPath()+"/";
        //将包名转换为文件名，然后和srcPath拼接
        String packagePath=DBHepler.getConf().getPoPackage().replace(".","/")+"/bean";
        File f=new File(srcPath+packagePath);
        if (!f.exists()){
            f.mkdirs();
        }
        BufferedWriter bw=null;
        try {
            String filePth = f.getAbsoluteFile()+"/"+StringUtils.underlineToBigCamel(tableInfo.getTname())+".java";
            File file2 = new File(filePth);
            if(file2.exists()) {
                return ;
            }
            //获取源码
            String src=createJavaSrc(tableInfo,convertor);
            //将源码写入文件
            bw=new BufferedWriter(new FileWriter(filePth));
            bw.write(src);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 更新表结构
     */
    public static void updateJavaPOFile(){
        Map<String,TableInfo>tables=TableContext.tables;
        for(TableInfo tableInfo:tables.values()){
            createJavaPoFile(tableInfo,new MySQLTypeConvertor());
        }
    }
}
